import AudioList from "@/utils/audio-list/main";
import { computed } from "vue";
import { useStore } from "vuex";

export function useAudio() {
  const store = useStore();
  const audio = computed(() => {
    return store.state.audio;
  });
  const paused = computed<boolean>(() => audio.value.paused);
  const info = computed(() => {
    return audio.value.info;
  });
  const playView = computed(() => audio.value.view);
  function prve() {
    // 上一曲
    AudioList.audioList.prev();
  }
  function next() {
    // 下一曲
    AudioList.audioList.next();
  }

  function play() {
    // 播放和暂停方法
    if (paused.value) {
      AudioList.audioList.play();
    } else {
      AudioList.audioList.pause();
    }
  }
  function pause() {
    AudioList.audioList.pause();
  }
  const musicName = computed(() => {
    if (info.value.mulu && info.value.xuhao) {
      return `${info.value.xuhao}.${info.value.mulu}`;
    }
    return "加载中...";
  });
  return {
    paused,
    info,
    prve,
    next,
    play,
    playView,
    audio,
    pause,
    musicName,
  };
}
